#define BLINKER_WIFI
#include <Blinker.h>
#define RGB_1 "RGBKey"
#define BUTTON_1 "ButtonKey"
#include "FastLED.h" // 此示例程序需要使用FastLED库
#define TEXTE_1 "TextKey"
#define NUM_LEDS 90      // LED灯珠数量
#define LED_DT 12        // Arduino输出控制信号引脚
#define LED_TYPE WS2812B // LED灯带型号
#define COLOR_ORDER GRB  // RGB灯珠中红色、绿色、蓝色LED的排列顺序
uint8_t set = 1, set1;
uint8_t r1 = 255, g1 = 255, b1 = 255, L1 = 255;
uint8_t fromcolor = 0, tocolor = 255, h = 20;
CRGB leds[NUM_LEDS]; // 建立光带leds
uint8_t colorIndex, jiezou = 10;
int a;

char auth[] = "b1e872088004";
char ssid[] = "2-19公共wifi";
char pswd[] = "12345678";

int data[10] = {0, 127, 213, 40, 200, 80, 160, 220, 100, 20};

BlinkerButton Button1("guan");
BlinkerButton Button2("kai");
BlinkerRGB RGB1("RGB_1");
BlinkerButton Button3("ok");
BlinkerSlider Slider1("from");
BlinkerSlider Slider3("set");
BlinkerSlider Slider2("to");
BlinkerSlider Slider4("jiezou");
BlinkerText Text1(TEXTE_1);
void button1_callback(const String &state)
{
  BLINKER_LOG("get button state: ", state);

  if (state == "tap")
  {
    set = 0;
    Button1.color("#000000");
    Button1.text("关灯");
    Button2.text("打开白光");
    Button2.print("off");
    Button1.print("off");
    Text1.print("", "");
    Blinker.print("off", "off");
  }
}

void button2_callback(const String &state)
{
  BLINKER_LOG("get button state: ", state);

  if (state == "tap")
  {
    set = 7;
    Button2.text("白光");
    Button2.print("on");
  }
}
void button3_callback(const String &state)
{
  BLINKER_LOG("get button state: ", state);

  if (state == "tap")
  {
    set = set1;
    Button3.text("已确定");
    Button3.print("on");
  }
}

void slider1_callback(int32_t value)
{
  BLINKER_LOG("get slider value: ", value);
  fromcolor = value;
}

void slider2_callback(int32_t value)
{
  BLINKER_LOG("get slider value: ", value);
  tocolor = value;
}
void slider4_callback(int32_t value)
{
  BLINKER_LOG("get slider value: ", value);
  jiezou = value;
}
void slider3_callback(int32_t value)
{
  BLINKER_LOG("get slider value: ", value);
  set1 = value;
  Button3.text("点我确定");
  Button3.print("on");
  switch (set1)
  {
  case 1:
    Text1.print("单色模式", "可通过色盘选择指定颜色，同时可调节对应亮度");
    Blinker.print("on", "ok");
    break;
  case 2:
    Text1.print("彩色模式", "可通过两个HSV色调滑块改变颜色，亮度通过色盘调节");
    Blinker.print("on", "ok");
    break;
  case 3:
    Text1.print("单色呼吸灯", "能够自动改变呼吸灯的颜色,灯光节奏滑块调节速度");
    Blinker.print("on", "ok");
    break;
  case 4:
    Text1.print("单色呼吸灯", "可以通过调色盘改变呼吸灯颜色 灯光节奏滑块调节速度");
    Blinker.print("on", "ok");
    break;
  case 5:
    Text1.print("流水灯", "可自动改变流水灯颜色 灯光节奏滑块调节速度");
    Blinker.print("on", "ok");
    break;
  case 6:
    Text1.print("跑马灯", "HSV色调滑块调节颜色，色盘调节亮度 灯光节奏滑块调节速度");
    Blinker.print("on", "ok");
    break;
  }
}
void rgb1_callback(uint8_t r_value, uint8_t g_value,
                   uint8_t b_value, uint8_t bright_value)
{
  digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
  BLINKER_LOG("R value: ", r_value);
  BLINKER_LOG("G value: ", g_value);
  BLINKER_LOG("B value: ", b_value);
  BLINKER_LOG("Rrightness value: ", bright_value);
  r1 = r_value;
  g1 = g_value;
  b1 = b_value;
  L1 = bright_value;
}
void dataRead(const String &data)
{
  BLINKER_LOG("Blinker readString: ", data);

  Blinker.vibrate();

  uint32_t BlinkerTime = millis();
  Blinker.print(BlinkerTime);
  Blinker.print("millis", BlinkerTime);
}

void setup()
{

  Serial.begin(115200);

  BLINKER_DEBUG.stream(Serial);
  // 初始化ws2812B灯带
  LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS);
  FastLED.setBrightness(255);
  // 初始化blinker
  Blinker.begin(auth, ssid, pswd);

  Blinker.attachData(dataRead);
  Button1.attach(button1_callback);
  Button2.attach(button2_callback);
  Button3.attach(button3_callback);

  RGB1.attach(rgb1_callback);
  Slider1.attach(slider1_callback);
  Slider2.attach(slider2_callback);
  Slider3.attach(slider3_callback);
  Slider4.attach(slider4_callback);
}

void loop()
{
  Blinker.run();
  switch (set)
  {
  case 0:
    FastLED.clear();
    FastLED.show();
    break;
  case 7:
    fill_solid(leds, NUM_LEDS, CRGB(255, 61, 62));
    FastLED.setBrightness(L1);
    FastLED.show();
    Blinker.delay(jiezou);
    break;
  case 1:
    fill_solid(leds, NUM_LEDS, CRGB(r1, g1, b1));
    FastLED.setBrightness(L1);
    FastLED.show();
    Blinker.delay(jiezou);
    break;
  case 4:
    for (int i = 30; i <= 255; i++)
    {
      fill_solid(leds, NUM_LEDS, CRGB(r1, g1, b1));
      FastLED.show();
      FastLED.setBrightness(i);
      Blinker.delay(jiezou);
      Blinker.run();
    }
    for (int m = 255; m >= 30; m--)
    {
      fill_solid(leds, NUM_LEDS, CRGB(r1, g1, b1));
      FastLED.show();
      FastLED.setBrightness(m);
      Blinker.delay(jiezou);
      Blinker.run();
    }
    break;

  case 3:
    h = h + 31;
    for (uint8_t i = 90; i <= 254; i++)
    {
      fill_gradient(leds, 0, CHSV(h, 255, i), NUM_LEDS - 1, CHSV(h, 255, i), LONGEST_HUES);
      Blinker.run();
      FastLED.show();
      Blinker.delay(jiezou);
    }
    for (uint8_t m = 254; m >= 90; m--)
    {
      fill_gradient(leds, 0, CHSV(h, 255, m), NUM_LEDS - 1, CHSV(h, 255, m), LONGEST_HUES);
      Blinker.run();
      FastLED.show();
      Blinker.delay(jiezou);
    }
    break;
  case 2:
    fill_gradient(leds, 0, CHSV(fromcolor, 255, L1), NUM_LEDS - 1, CHSV(tocolor, 255, L1), LONGEST_HUES);
    FastLED.show();
    Blinker.run();
    break;
  case 5:
    int n;
    uint8_t v;
    h = h + 40;
    for (n = 0; n <= (NUM_LEDS - 1); n++)
    {
      fill_gradient(leds, n, CHSV(h, 255, L1), n, CHSV(h, 255, L1), LONGEST_HUES);
      FastLED.show();
      Blinker.delay(jiezou);
      Blinker.run();
    }
    for (n = NUM_LEDS - 1; n >= 0; n--)
    {
      fill_gradient(leds, n, CHSV(h + 100, 255, L1), n, CHSV(h + 100, 255, L1), LONGEST_HUES);
      FastLED.show();
      Blinker.delay(jiezou);
      Blinker.run();
    }
    break;
  case 6:
    for (uint8_t n = 0; n <= NUM_LEDS - 10; n++)
    {
      fill_gradient(leds, n, CHSV(fromcolor, 255, L1), n + 9, CHSV(tocolor, 255, L1), SHORTEST_HUES);
      FastLED.show();
      Blinker.delay(jiezou);
      Blinker.run();
      FastLED.clear();
    }
    for (uint8_t h = NUM_LEDS - 1; h >= 9; h--)
    {
      fill_gradient(leds, h, CHSV(fromcolor, 255, L1), h - 9, CHSV(tocolor, 255, L1), SHORTEST_HUES);
      FastLED.show();
      Blinker.delay(jiezou);
      Blinker.run();
      FastLED.clear();
    }
    break;
  }
}